# -*- coding: utf-8 -*-
import socket
import sys
from PyQt4.QtGui import (
    QApplication,
    QGridLayout,
    QTextEdit,
    QWidget,QTabWidget,QFrame,
    QTextBrowser,QProgressBar,
    QPushButton,QCheckBox,
    QTableWidget,QTableWidgetItem,
    QSplitter,
    QScrollArea
)
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class HexEdit(object):
    '''HEX编辑器'''
    def __init__(self,parent=None,edit=False):
        super(TabRequest,self).__init__(parent)

class Button(QPushButton):
    '''按钮'''
    def __init__(self,parent=None):
        super(Button,self).__init__(parent)
        '''
        background-color: rgb(20, 200, 136);
        font: 15pt "微软雅黑";
        color: rgb(255, 255, 255);
        border-radius: 10px;
        '''
    def setpos(self,x,y,w=75,h=33):
        self.setGeometry(QtCore.QRect(x, y, w, h))

class Frame(QFrame):
    '''按钮'''
    def __init__(self,parent=None):
        super(Frame,self).__init__(parent)
        #self.setFrameShape(QFrame.NoFrame)

class TextEdit(QTextEdit):
    '''文本编辑器'''
    def __init__(self,parent=None):
        super(TextEdit,self).__init__(parent)

class TextView(QTextBrowser):
    '''文本查看器'''
    def __init__(self,parent=None):
        super(TextView,self).__init__(parent)

class EditWidget(QWidget):
    '''(request,response)查看/编辑'''
    def __init__(self,parent=None,edit=False):
        super(EditWidget,self).__init__(parent)
        if edit:
            self.textEdit= TextEdit(self)
        else:
            self.textEdit = TextView(self)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.textEdit)

class ViewWidget(QTabWidget):
    '''结果查看器'''
    def __init__(self,parent=None):
        super(ViewWidget,self).__init__(parent)
        self.TabRequest = EditWidget(self)
        self.addTab(self.TabRequest,None)
        self.setTabText(0, _translate("Form", "请求", None))
        self.TabResponse = EditWidget()
        self.addTab(self.TabResponse,None)
        self.setTabText(1, _translate("Form", "返回", None))
        self.setCurrentIndex(0)


class SettingWidget(QWidget):
    '''设置页面基类'''
    def __init__(self,parent=None):
        super(SettingWidget,self).__init__(parent)
        self.scrollArea = QtGui.QScrollArea(self)
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAsNeeded)
        self.scrollArea.setWidgetResizable(True)
        self.scrollAreaWidgetContents = QWidget()
        self.scrollArea.setWidget(self.scrollAreaWidgetContents)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)

    def addWidget(self,*widgets):
        self.gridLayout = QGridLayout(self.scrollAreaWidgetContents)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        for i,widget in enumerate(widgets):
            self.gridLayout.addWidget(widget, i, 0, 1, 1)

class HistoryWidget(QWidget):
    '''历史页面基类'''
    def __init__(self,parent=None):
        super(HistoryWidget,self).__init__(parent)
        self.splitter = QSplitter(self)
        self.splitter.setOrientation(QtCore.Qt.Vertical)
        self.tableWidget = QTableWidget(self.splitter)
        self.tableWidget.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
        self.viewWidget = ViewWidget(self.splitter)
        self.viewWidget.setTabPosition(QTabWidget.North)
        self.viewWidget.setTabShape(QTabWidget.Rounded)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.splitter, 0, 0, 1, 1)

    def setTitle(self,title=[]):
        self.tableWidget.setAutoScrollMargin(8)
        self.tableWidget.setColumnCount(len(title))
        self.tableWidget.setRowCount(0)
        for i,value in enumerate(title):
            item = QtGui.QTableWidgetItem()
            item.setText(_translate("Form", value, None))
            self.tableWidget.setHorizontalHeaderItem(i, item)

##############################################################
class ProxyTabContent(QWidget):
    '''代理内容窗口'''
    def __init__(self,parent=None):
        super(ProxyTabContent,self).__init__(parent)
        self.textEdit = EditWidget(self,True)
        self.tabAction = Frame(self)
        self.tabAction.setMinimumSize(QtCore.QSize(0, 40))
        self.tabAction.setMaximumSize(QtCore.QSize(16777215, 40))
        self.tabAction.setFrameShape(QFrame.StyledPanel)
        self.tabAction.setFrameShadow(QFrame.Raised)
        self.checkBoxResuest = QCheckBox(self.tabAction)
        self.checkBoxResuest.setGeometry(QtCore.QRect(2, 2, 70, 16))
        self.checkBoxResuest.setText(_translate("Form", "拦截请求", None))
        self.checkBoxResuest.setCheckState(QtCore.Qt.Checked)
        self.checkBoxResponse = QCheckBox(self.tabAction)
        self.checkBoxResponse.setGeometry(QtCore.QRect(2, 22, 70, 16))
        self.checkBoxResponse.setText(_translate("Form", "拦截返回", None))
        self.pushButtonDrop = Button(self.tabAction)
        self.pushButtonDrop.setpos(98, 4,)
        self.pushButtonDrop.setText(_translate("Form", "拦截", None))
        self.pushButtonForward = Button(self.tabAction)
        self.pushButtonForward.setpos(184, 4)
        self.pushButtonForward.setText(_translate("Form", "放行", None))
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.textEdit, 0, 0, 1, 1)
        self.gridLayout.addWidget(self.tabAction, 1, 0, 1, 1)

class ProxyTabSetting(QWidget):
    '''代理设置窗口'''
    def __init__(self,parent=None):
        super(ProxyTabSetting,self).__init__(parent)
        self.SettingWidget = SettingWidget(self)
        self.SettingWidget.addWidget(
            EditWidget(self.SettingWidget,True),
        )
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.SettingWidget, 0, 0, 1, 1)

class ProxyTabs(QTabWidget):
    '''代理窗口'''
    def __init__(self,parent=None):
        super(ProxyTabs,self).__init__(parent)
        self.ProxyTabContent = ProxyTabContent()
        self.addTab(self.ProxyTabContent,None)
        self.setTabText(0, _translate("Form", "内容", None))
        self.ProxyTabHistory = HistoryWidget()
        self.ProxyTabHistory.setTitle(['序号','主机','方法','URL','状态','长度','类型'])
        self.addTab(self.ProxyTabHistory,None)
        self.setTabText(1, _translate("Form", "历史", None))
        self.ProxyTabSetting = ProxyTabSetting()
        self.addTab(self.ProxyTabSetting,None)
        self.setTabText(2, _translate("Form", "选项", None))

###########################################################################
class RepostTab(QWidget):
    '''重放子窗口'''
    def __init__(self,parent=None):
        super(RepostTab,self).__init__(parent)
        self.splitter = QSplitter(self)
        self.splitter.setOrientation(QtCore.Qt.Horizontal)
        self.textEdit = EditWidget(self.splitter,True)
        self.textView = EditWidget(self.splitter)
        self.frame = Frame(self)
        self.frame.setMinimumSize(QtCore.QSize(0, 40))
        self.frame.setMaximumSize(QtCore.QSize(16777215, 40))
        self.frame.setFrameShape(QFrame.StyledPanel)
        self.frame.setFrameShadow(QFrame.Raised)
        self.pushButtonsend = Button(self.frame)
        self.pushButtonsend.setpos(12,4)
        self.pushButtonsend.setText(_translate("Form", "发送", None))
        self.pushButtoncancel = Button(self.frame)
        self.pushButtoncancel.setpos(106, 4)
        self.pushButtoncancel.setText(_translate("Form", "取消", None))
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.splitter, 0, 0, 1, 1)
        self.gridLayout.addWidget(self.frame, 1, 0, 1, 1)
        ######################
        self.host = None
        self.port = None

    def send(self):
        if self.host and self.port:
            pass


class RepostTabs(QTabWidget):
    '''重放窗口'''
    def __init__(self,parent=None):
        super(RepostTabs,self).__init__(parent)
        self.setTabsClosable(True)
        self.RepostTab = RepostTab(self)
        self.addTab(self.RepostTab,None)
        self.setTabText(0, _translate("Form", "1", None))


###########################################################################
class BurteTabSetting(QWidget):
    def __init__(self,parent=None):
        super(BurteTabSetting,self).__init__(parent)
        self.SettingWidget = SettingWidget(self)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.SettingWidget, 0, 0, 1, 1)

class BurteTab(QWidget):
    '''爆破子窗口'''
    def __init__(self,parent=None):
        super(BurteTab,self).__init__(parent)
        self.tabWidget = QtGui.QTabWidget(self)
        self.tabContent = QWidget(self.tabWidget)
        self.tabSetting = SettingWidget(self.tabWidget)
        self.tabResult = HistoryWidget(self.tabWidget)
        self.tabResult.setTitle(['序号','主机','方法','URL','状态','长度','类型'])
        self.tabWidget.addTab(self.tabContent,None)
        self.tabWidget.setTabText(0, _translate("Form", "内容", None))
        self.tabWidget.addTab(self.tabSetting,None)
        self.tabWidget.setTabText(1, _translate("Form", "载体", None))
        self.tabWidget.addTab(self.tabResult,None)
        self.tabWidget.setTabText(2, _translate("Form", "结果", None))
        self.frameright = Frame(self.tabContent)
        self.frameright.setMinimumSize(QtCore.QSize(70, 0))
        self.frameright.setMaximumSize(QtCore.QSize(70, 16777215))
        self.frameright.setFrameShape(QFrame.StyledPanel)
        self.frameright.setFrameShadow(QFrame.Raised)
        self.pushButtonadd = Button(self.frameright)
        self.pushButtonadd.setpos(10, 16)
        self.pushButtonadd.setText(_translate("Form", "增加", None))
        self.pushButtonclear = Button(self.frameright)
        self.pushButtonclear.setpos(10, 60)
        self.pushButtonclear.setText(_translate("Form", "清除", None))
        self.textEdit = EditWidget(self.tabContent,True)
        self.gridLayoutcontent = QGridLayout(self.tabContent)
        self.gridLayoutcontent.setMargin(0)
        self.gridLayoutcontent.setSpacing(0)
        self.gridLayoutcontent.addWidget(self.textEdit, 0, 0, 1, 1)
        self.gridLayoutcontent.addWidget(self.frameright, 0, 1, 1, 1)
        self.frameaction = Frame(self)
        self.frameaction.setMinimumSize(QtCore.QSize(0, 40))
        self.frameaction.setMaximumSize(QtCore.QSize(16777215, 40))
        self.frameaction.setFrameShape(QFrame.StyledPanel)
        self.frameaction.setFrameShadow(QFrame.Raised)
        self.pushButtonstart = Button(self.frameaction)
        self.pushButtonstart.setpos(12,4)
        self.pushButtonstart.setText(_translate("Form", "开始", None))
        '''
        self.progressBar = QProgressBar(self.frameaction)
        self.progressBar.setProperty("value", 24)
        self.progressBar.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
        self.progressBar.setOrientation(QtCore.Qt.Horizontal)
        self.progressBar.setInvertedAppearance(False)
        self.progressBar.setTextDirection(QProgressBar.TopToBottom)
        '''
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.tabWidget, 0, 0, 1, 1)
        self.gridLayout.addWidget(self.frameaction, 1, 0, 1, 1)

class BurteTabs(QTabWidget):
    '''爆破窗口'''
    def __init__(self,parent=None):
        super(BurteTabs,self).__init__(parent)
        self.setTabsClosable(True)
        self.BurteTab = BurteTab(self)
        self.addTab(self.BurteTab,None)
        self.setTabText(0, _translate("Form", "1", None))

###########################################################################

class SettingTabs(QWidget):
    '''设置窗口'''
    def __init__(self,parent=None):
        super(SettingTabs,self).__init__(parent)
        self.SettingWidget = SettingWidget(self)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.SettingWidget, 0, 0, 1, 1)

###########################################################################
#############################菜刀##############################################
class CaidaoTabs(QWidget):
    '''菜刀窗口'''
    def __init__(self,parent=None):
        super(CaidaoTabs,self).__init__(parent)
        self.SettingWidget = SettingWidget(self)
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.SettingWidget, 0, 0, 1, 1)

###########################################################################
class MainTabs(QTabWidget):
    '''主窗口'''
    def __init__(self,parent=None):
        super(MainTabs,self).__init__(parent)
        self.setTabPosition(QTabWidget.West)     #竖向
        self.setTabShape(QTabWidget.Triangular)
        self.ProxyTabs = ProxyTabs()
        self.addTab(self.ProxyTabs,None)
        self.setTabText(0, _translate("Form", "代理", None))
        self.RepostTabs = RepostTabs()
        self.addTab(self.RepostTabs,None)
        self.setTabText(1, _translate("Form", "重放", None))
        self.BurteTabs = BurteTabs()
        self.addTab(self.BurteTabs,None)
        self.setTabText(2, _translate("Form", "爆破", None))
        self.SettingTabs = SettingTabs()
        self.addTab(self.SettingTabs,None)
        self.setTabText(3, _translate("Form", "设置", None))
##############################################################################
class MainWin(QWidget):
    def __init__( self):
        super(MainWin, self).__init__()
        self.setWindowTitle("hello")
        self.resize(800,500)
        self.MainTabs = MainTabs()
        self.gridLayout = QGridLayout(self)
        self.gridLayout.setMargin(0)
        self.gridLayout.setSpacing(0)
        self.gridLayout.addWidget(self.MainTabs, 0, 0, 1, 1)

if __name__=='__main__':
   def main():
       app = QApplication(sys.argv)
       mainWindow = MainWin()
       mainWindow.show()
       sys.exit(app.exec_())
   main()